package org.example;

import javax.annotation.processing.Filer;
import java.io.*;
import java.util.*;

public class IO_Demo14 {
    public static void main(String[] args) throws IOException {
        //把《出师表》的文章顺序进行回复到一个新文件中

        //1.读取数据
        BufferedReader fr=new BufferedReader(new InputStreamReader(new FileInputStream("文件路径")));
        String line;
        ArrayList<String> list=new ArrayList<>();
        while((line=fr.readLine())!=null){
           list.add(line);
        }
        fr.close();
        //排序
        Collections.sort(list,new Comparator<String>() {
            @Override
            public int compare(String o1, String o2) {
                int i1=Integer.parseInt(o1.split("\\.")[0]);
                int i2=Integer.parseInt(o2.split("\\.")[0]);
                return i1-i2;
            }
        });
        //写出数据
        BufferedWriter bw=new BufferedWriter(new FileWriter("目标文件路径"));
        for (String s : list) {
            bw.write(s);
            bw.newLine();
        }
        bw.close();
        //第二种写法
        BufferedReader br1=new BufferedReader(new FileReader("文件路径"));
        String line1;
        TreeMap<Integer,String> tm=new TreeMap<>();
        while((line1=br1.readLine())!=null){
            String[] s1=line1.split("\\.");
            int k=Integer.parseInt(s1[0]);
            tm.put(k,s1[1]);
        }
        br1.close();
        BufferedWriter bw1=new BufferedWriter(new FileWriter("目标文件路径"));
        //键值对对象
        Set<Map.Entry<Integer,String>>entries=tm.entrySet();
        for (Map.Entry<Integer, String> entry : entries) {
            bw1.write(entry.getValue());
            bw1.newLine();//换行
        }
        bw1.close();
    }
}
